package leetcode2;

public class GarbageCollection {
    public int garbageCollection(String[] garbage, int[] travel) {
        int last_m = 0, last_p = 0, last_g = 0;
        int m_count = 0, p_count = 0, g_count = 0;
        for (int i = 0; i < garbage.length; i++) {
            for (int j = 0; j < garbage[i].length(); j++) {
                char c = garbage[i].charAt(j);
                if (c == 'M') {
                    m_count++;
                    last_m = i;
                }
                if (c == 'P') {
                    p_count++;
                    last_p = i;
                }
                if (c == 'G') {
                    g_count++;
                    last_g = i;
                }
            }
        }
        int ans = m_count + p_count + g_count;
        for (int i = 0; i < travel.length; i++) {
            if (last_m > i) {
                ans += travel[i];
            }
            if (last_p > i) {
                ans += travel[i];
            }
            if (last_g > i) {
                ans += travel[i];
            }
        }
        return ans;
    }
}
